
package org.jetnuke.dao.generic;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import net.jforum.JForumExecutionContext;
import net.jforum.dao.generic.AutoKeys;
import net.jforum.util.preferences.SystemGlobals;

import org.jetnuke.entities.Modules;

public class GenericModuleDAO extends AutoKeys implements
		org.jetnuke.dao.ModuleDAO {

	/* (non-Javadoc)
	 * @see org.jetnuke.model.ModuleModel#retrieveByPK(int)
	 */
	public Modules retrieveByPK(int id) throws Exception{
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("ModuleModel.selectByID"));
		p.setInt(1, id);
		
		ResultSet rs = p.executeQuery();
		Modules s = new Modules();
		
		if (rs.next()) {
			fillModuleFromResultSet(s, rs);
		}
		
		rs.close();
		p.close();

		return s;
	}

	/**
	 * @param s
	 * @param rs
	 */
	private void fillModuleFromResultSet(Modules s, ResultSet rs) throws Exception{
		s.setACTIVE(rs.getInt("ACTIVE"));
		s.setBKEY(rs.getString("BKEY"));
		s.setINMENU(rs.getInt("INMENU"));
		s.setMID(rs.getInt("MID"));
		s.setPATH(rs.getString("PATH"));
		s.setTITLE(rs.getString("TITLE"));
		s.setTITLE_I18N(rs.getString("TITLE_I18N"));
		
	}

	protected List processSelectAll(ResultSet rs)throws Exception {
		List list = new ArrayList();
		while (rs.next()) {
			Modules u = new Modules();
			fillModuleFromResultSet(u,rs);			
			list.add(u);
		}
		
		return list;
	}
	public List selectAll() throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("ModuleModel.selectAll"));
			
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.ModuleModel#update(org.jetnuke.entities.Modules)
	 */
	public void update(Modules module) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("ModuleModel.update"));
		p.setInt(1,module.getACTIVE());
		p.setString(2,module.getBKEY());
		p.setInt(3,module.getINMENU());
		p.setString(4,module.getPATH());
		p.setString(5,module.getTITLE());
		p.setString(6,module.getTITLE_I18N());
		p.setInt(7,module.getMID());
		
		p.executeUpdate();
		p.close();
		
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.ModuleModel#selectAllActiveInMenu()
	 */
	public List selectAllActiveInMenu() throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("ModuleModel.selectAllActiveInMenu"));
		
	ResultSet rs = p.executeQuery();
	List list = this.processSelectAll(rs);
	rs.close();
	p.close();
	
	return list;
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.ModuleModel#selectAllInactive()
	 */
	public List selectAllInactive() throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("ModuleModel.selectAllInactive"));
		
	ResultSet rs = p.executeQuery();
	List list = this.processSelectAll(rs);
	rs.close();
	p.close();
	
	return list;
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.ModuleModel#selectAllActiveNotMenu()
	 */
	public List selectAllActiveNotMenu() throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("ModuleModel.selectAllActiveNotMenu"));
		
	ResultSet rs = p.executeQuery();
	List list = this.processSelectAll(rs);
	rs.close();
	p.close();
	
	return list;
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.ModuleModel#deleteByPK(int)
	 */
	public void deleteByPK(int mid) throws Exception {
		PreparedStatement p= JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("ModuleModel.deleteByPK"));
		p.setInt(1,mid);
		p.executeUpdate();
		p.close();
		
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.ModuleModel#addNew(org.jetnuke.entities.Modules)
	 */
	public int addNew(Modules module) throws Exception {
		PreparedStatement p = this.getStatementForAutoKeys("ModuleModel.addNew");
		this.setAutoGeneratedKeysQuery(SystemGlobals.getSql("ModuleModel.lastGeneratedModuleID"));
		initNewModule(module, p);
		
		int id = this.executeAutoKeysQuery(p);
				
		return id;
		
	}

	/**
	 * @param module
	 * @param p
	 */
	private void initNewModule(Modules module, PreparedStatement p) throws Exception {
		p.setInt(1,module.getACTIVE());
		p.setString(2,module.getBKEY());
		p.setInt(3,module.getINMENU());
		p.setString(4,module.getPATH());
		p.setString(5,module.getTITLE());
		p.setString(6,module.getTITLE_I18N());
		
	}


}
